package com.app.controller;

import com.alibaba.fastjson.JSON;
import com.app.model.response.*;
import com.app.service.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by Lichaojie on 2017/3/4. It's a beautiful day.
 */
@RequestMapping(value = "/user")//produces = "application/json;charset=utf-8"
@RestController
public class UserController {

    private static Log log = LogFactory.getLog(UserController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private RecommendService recommendService;
    @Autowired
    private SearchService searchService;

    /**
     * http://localhost:8080/user/test?name=
     * @RequestParam 用来处理 Content-Type: 为 application/x-www-form-urlencoded编码的内容，提交方式GET、POST
     * @RequestBody 用来处理 Content-Type: 不为 application/x-www-form-urlencoded编码的内容，
     * @param name
     * @return
     */
    @RequestMapping(value = "/test")
    public String hello(@RequestParam String name){
        Map result = new HashMap();
        result.put("Hello,",name);

        System.currentTimeMillis();

        return JSON.toJSONString(result);
        //return JSON.toJSONString(response);

    }


    /**
     * 注册
     * @param
     * @return
     */
    @RequestMapping(value = "/doRegister" )
    public RetStatus doRegister(@RequestBody String parameter){
        return userService.doRegister(parameter);
    }
//    public void doRegister(@RequestBody ReceiverClass receiverClass){
//        System.out.println("username" + receiverClass.getUserName() + "\n" + "password" + receiverClass.getPassword());
//    }



    /**
     * 第三方登录
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/doThirdRegister")
    public RetStatus doThirdRegister(@RequestBody String parameter){
        return userService.doThirdRegister(parameter);
    }

    /**
     * 验证手机号
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/checkPhone")
    public RetStatus checkPhone(@RequestBody String parameter){
        return  userService.checkPhone(parameter);
    }

    /**
     * 验证第三方账号是否使用过
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/checkUid")
    public RetStatus checkUid(@RequestBody String parameter){
        return userService.checkUid(parameter);
    }

    /**
     * 验证token
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/checkIdentity")
    public RetStatus checkIdentity(@RequestBody String parameter){
        return userService.checkIdentity(parameter);
    }

    /**
     * 用户名&密码登录
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/doLogin")
    public RetStatus doLogin(@RequestBody String parameter){
        return userService.doLogin(parameter);
    }

    /**
     * 完善个人信息
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/completeUserInfo")
    public RetStatus completeUserInfo(@RequestBody String parameter) throws Exception{
        return userService.completeUserInfo(parameter);
    }

    /**
     * 修改个人信息
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/modifyUserInfo")
    public RetStatus modifyUserInfo(@RequestBody String parameter) throws Exception{
        return userService.completeUserInfo(parameter);
    }

    @RequestMapping(value = "/getUserInfo")
    public RetStatus getUserInfo(@RequestBody String parameter){
        return userService.getUserInfo(parameter);
    }


    //TODO 获取关系
    /**
     *
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/relation")
    public RetStatus getUserRelation(@RequestBody String parameter){
        return null;
    }


    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public RetStatus getUserDetails(@RequestParam("userId") long userId){
        return userService.getUserDetails(userId);
    }

    @RequestMapping(value = "/fans",method = RequestMethod.GET)
    public RetStatus getFansList(@RequestParam("userId") long userId,
                                 @RequestParam("lastId") long lastId,
                                 @RequestParam("number") int number,
                                 @RequestParam("type") byte type){
        return userService.getFansList(userId,lastId,number,type);
    }

    @RequestMapping(value = "/focus",method = RequestMethod.GET)
    public RetStatus getFocusList(@RequestParam("userId") long userId,
                                  @RequestParam("lastId") long lastId,
                                  @RequestParam("number") int number,
                                  @RequestParam("type") byte type){
        return userService.getFocusList(userId,lastId,number,type);
    }

    @RequestMapping(value = "/other/detail",method = RequestMethod.GET)
    public RetStatus getOtherDetail(@RequestParam("myId")long myId,
                                    @RequestParam("otherId")long otherId){
        return userService.getOtherDetails(myId,otherId);
    }


    /**
     * 重置密码
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/resetPassword")
    public RetStatus resetPassword(@RequestBody String parameter){
        return userService.resetPassword(parameter);
    }

    /**
     * 修改密码
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/modifyPassword")
    public RetStatus modifyPassword(@RequestBody String parameter){
        return userService.modifyPassword(parameter);
    }


    /**
     * 搜索用户
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/doSearch")
    public RetStatus doSearch(@RequestBody String parameter){
        //return userService.doSearch(parameter);
        return searchService.doSearch(parameter);
    }

    /**
     * 搜索附近的用户
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/doSearchWithDistance")
    public RetStatus doSearchWithDistance(@RequestBody String parameter){
        //return userService.doSearchWithDistance(parameter);
        return searchService.doSearchWithDistance(parameter);
    }


    /**
     * 推荐用户（加载更多）
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/getRecommend/LoadMore")
    public RetStatus getRecommendLoadMore(@RequestBody String parameter){
        //return userService.getRecommendLoadMore(parameter);
        return recommendService.getUserRecommendLoadMore(parameter);
    }

    /**
     * 推荐用户（刷新）
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/getRecommend/Update")
    public RetStatus getRecommendUpdate(@RequestBody String parameter){
        //return userService.getRecommendUpdate(parameter);
        return recommendService.getUserRecommendUpdate(parameter);
    }

    /**
     * 推荐附近用户（加载更多）
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/getRecommendWithDistance/LoadMore")
    public RetStatus getRecommendWithDistanceLoadMore(@RequestBody String parameter){
        return userService.getRecommendWithDistanceLoadMore(parameter);
    }

    /**
     * 推荐附近用户（刷新）
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/getRecommendWithDistance/Update")
    public RetStatus getRecommendWithDistanceUpdate(@RequestBody String parameter){
        return userService.getRecommendWithDistanceUpdate(parameter);
    }

    @RequestMapping(value = "/setPosition")
    public RetStatus setPosition(@RequestBody String parameter){
        return userService.setPosition(parameter);
    }

    @RequestMapping(value = "/checkStatus")
    public RetStatus checkStatus(@RequestBody String parameter){
        return userService.checkStatus(parameter);
    }

    /**
     * 获取我的发帖数量
     * @param parameter
     * @return
     */
    @RequestMapping(value = "/getMyArticleNum")
    public RetStatus getMyArticleNum(@RequestBody String parameter){
        return userService.getMyArticleNum(parameter);
    }

    /**
     * 用户主动退出登录
     * @param parameter
     * @return
     */
    @RequestMapping(value = "doLogout")
    public RetStatus doLogout(@RequestBody String parameter){
        return userService.logout(parameter);
    }

    /**
     * 用户离开应用，重新进入时也需要登录。
     * 具体操作同上
     * @param parameter
     * @return
     */
    @RequestMapping(value = "leaveApp")
    public RetStatus leaveApp(@RequestBody String parameter){
        return userService.logout(parameter);
    }

}
